package com.cx.style;

import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package com.cx.style
 * @Description:
 * @Date 2025/5/26 18:59
 */

public class LazySingleton {
    private static LazySingleton instance;

    private LazySingleton(){
        System.out.println("懒汉式延迟加载");

    }
    public static synchronized LazySingleton getInstance(){
        if(instance == null ){
            instance = new LazySingleton();
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i <10 ; i++) {
            new Thread(()->{
                HolderSingleton instance1 = HolderSingleton.getInstance();
                System.out.println(instance1);

            }).start();

        }
    }
}

class HungrySingleton{
    private final static HungrySingleton instance = new HungrySingleton();

    private HungrySingleton(){
        System.out.println("饿汉式加载");

    }

    public static  HungrySingleton getInstance(){
        return instance;
    }
}

class HolderSingleton{

    private HolderSingleton(){
        System.out.println("静态方法式加载");
    }
    private static class holder{
        private static final  HolderSingleton SINGLETON = new HolderSingleton();

    }

    public static HolderSingleton getInstance(){
        return holder.SINGLETON;
    }
}